<route lang="json5">
{
  style: {
    navigationBarTitleText: '政策解读',
  },
}
</route>

<template>
  <view class="common-tab-list-wrap">
    <wd-tabs
      custom-class="common-header-tabs"
      v-model="tab"
      @change="changeTab"
    >
      <block
        v-for="(item, index) in tabList"
        :key="index"
      >
        <wd-tab
          :title="item.label"
          :name="item.value"
        ></wd-tab>
      </block>
    </wd-tabs>
    <view class="content">
      <scroll-view
        scroll-y
        class="scroll-view flex-1"
        refresher-enabled
        enable-back-to-top
        :refresher-triggered="refresherTriggered"
        @refresherrefresh="onRefresherrefresh"
        @scrolltolower="getDataList"
      >
        <view class="data-list">
          <template v-if="dataList.length">
            <view
              v-for="item in dataList"
              :key="item.id"
              class="data-item"
              @click="
                uniNavigateTo(
                  `/pages-sub/policy-interpret/detail/index?id=${item.id}`,
                )
              "
            >
              <view class="data-item-header">
                <text class="type">【{{ item.explainType_dictText }}】</text>
                <text class="title">{{ item.title }}</text>
              </view>
              <view class="data-item-row">
                发文单位：{{ item.publishUnit }}
              </view>
              <view class="data-item-row">
                发文日期：{{ item.publishDate }}
              </view>
            </view>
          </template>
          <template v-else>
            <wd-status-tip
              image="content"
              tip="暂无内容"
            />
          </template>
        </view>
        <wd-loadmore
          v-if="dataList.length"
          finished-text="没有更多了"
          :state="loadState"
          @reload="getDataList"
        />
      </scroll-view>
    </view>
  </view>
</template>

<script setup>
import PolicyApi from '@/service/policy'
// import { useDictStore } from '@/store/dict'
import { uniNavigateTo } from '@/utils'
import { getDictDetail } from '@/service/dict'

// const { dictData } = useDictStore()

const tabList = ref([])
const tab = ref('')

const getTabData = async () => {
  const { code, result } = await getDictDetail('policyType')
  if (code === 200 && result) {
    tabList.value = [...result]
    tab.value = tabList.value[0].value
    console.log('tabList.value', tabList.value)
    console.log('tab.value', tab.value)
  }
}

const changeTab = (event) => {
  console.log('event change', event)
  console.log('changeTab', tab.value)
  tab.value = event.name
  refreshList()
}

/** 列表分页操作 start */
const loadState = ref('loading')
const pageObj = ref({
  pageNo: 1,
  pageSize: 10,
})
const dataList = ref([])

const getDataList = async () => {
  if (loadState.value === 'finished') return
  try {
    const params = {
      ...pageObj.value,
      type: tab.value,
      column: 'createTime',
      order: 'desc',
    }
    const { code, result } = await PolicyApi.getPolicyList(params)
    if (code === 200 && result) {
      const { records, pages } = result
      if (records?.length) {
        dataList.value = [...dataList.value, ...records]
      }
      if (pageObj.value.pageNo < pages) {
        pageObj.value.pageNo++
      } else {
        loadState.value = 'finished'
      }
    } else {
      loadState.value = 'error'
    }
  } catch (e) {
    loadState.value = 'error'
  }
}

const reset = () => {
  pageObj.value.pageNo = 1
  dataList.value = []
  loadState.value = 'loading'
}

const refreshList = () => {
  reset()
  getDataList()
}

const refresherTriggered = ref(false)
const onRefresherrefresh = async () => {
  refresherTriggered.value = true
  // 重置分页数据，刷新接口
  reset()
  await getDataList()
  refresherTriggered.value = false
}
/** 列表分页操作 end */

onLoad(async () => {
  await getTabData()
  refreshList()
})
</script>

<style lang="scss" scoped>
.data-list {
  padding: $uni-spacing-col-lg $uni-spacing-row-lg;

  .data-item {
    padding-bottom: $uni-spacing-col-lg;
    margin-bottom: $uni-spacing-col-lg;
    border-bottom: 1px solid $uni-border-color;

    &:last-child {
      border-bottom: 0;
    }

    &-header {
      margin-bottom: $uni-spacing-col-base;

      .type {
        color: $uni-color-primary;
      }

      .title {
        font-weight: 600;
      }
    }

    &-row {
      margin-bottom: $uni-spacing-col-sm;
      color: $uni-text-color-grey;
    }
  }
}
</style>
